#include "mypushbutton.h"
#include "mypushbutton.h"
#include<QPropertyAnimation>
#include<QEasingCurve>

MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
    //保存正常显示路径和选中后显示的路径
    this->normalImg = normalImg;
    this->pressImg = pressImg;

    QPixmap pix;
    bool ret = pix.load(this->normalImg);
    if (!ret) {
        QString str = QString("%1 图片加载失败").arg(this->normalImg);
        qDebug() << str;
        return;
    }

    //设定图片大小
    this->setFixedSize(pix.width(), pix.height());

    //设置不规则图片样式
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图片
    this->setIcon(pix);

    //设置图片大小
    this->setIconSize(QSize(pix.width(), pix.height()));

}

void MyPushButton::Zoom1()
{
    QPropertyAnimation * animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(200);

    animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
    animation->setEndValue(QRect(this->x(), this->y() + 10, this->width(), this->height()));

    //设置曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    animation->start();
}

void MyPushButton::Zoom2()
{
    QPropertyAnimation * animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(200);

    animation->setStartValue(QRect(this->x(), this->y() + 10, this->width(), this->height()));
    animation->setEndValue(QRect(this->x(), this->y(), this->width(), this->height()));

    //设置曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    animation->start();
}


//鼠标按下
void MyPushButton::mousePressEvent(QMouseEvent *e)
{
    if (this->pressImg != "") { //传入选中后的图片路径不空，需要选中后来选中图片的切换
        QPixmap pix;
        bool ret = pix.load(this->pressImg);
       if(!ret) {
           qDebug() << "图片加载失败";
           return;
       }

       //设定图片大小
       this->setFixedSize(pix.width(), pix.height());

       //设置不规则图片样式
       this->setStyleSheet("QPushButton{border:0px;}");

       //设置图片
       this->setIcon(pix);

       //设置图片大小
       this->setIconSize(QSize(pix.width(), pix.height()));
    }

    //其他事情交给父类处理
    QPushButton::mousePressEvent(e);
}

//鼠标释放
void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if (this->pressImg != "") { //传入选中后的图片路径不空，需要选中后来选中图片的切换
        QPixmap pix;
        bool ret = pix.load(this->normalImg);
       if(!ret) {
           qDebug() << "图片加载失败";
           return;
       }

       //设定图片大小
       this->setFixedSize(pix.width(), pix.height());

       //设置不规则图片样式
       this->setStyleSheet("QPushButton{border:0px;}");

       //设置图片
       this->setIcon(pix);

       //设置图片大小
       this->setIconSize(QSize(pix.width(), pix.height()));
    }

    //其他事情交给父类处理
    QPushButton::mouseReleaseEvent(e);
}
